<template>
  <div>
    <div @keyup.enter="getData()">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="params.customer_name" placeholder="项目名称查询" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="params.corp_name"
            placeholder="负责人查询"
            :clearable="true"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <select-address
            v-model="params.address_code"
            clearable
            placeholder="地址查询"
            :auto-top="true"
            @change="getData()"
          ></select-address>
        </el-form-item>
        <el-form-item>
          <c-select
            :options="enumSignType"
            v-model="params['sign_type.eq']"
            clearable
            placeholder="签章类型"
            @change="getData()"
          ></c-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getData()">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <c-table
      border
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
      :height="dialogHeight"
    ></c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <el-dialog
      :visible.sync="viewVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="900px"
      class="dialog-sm"
    >
      <pdf-view :down-name="downName" :contract-id="docId" :query-id="queryId" view-mode></pdf-view>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectAddress from '@/components/select/SelectAddress'
import PdfView from '@/credit/views/county/unionProvince/components/PdfView'
export const enumSignType = {
  1: '电子保函',
  2: '联保函',
}
export default {
  components: {
    SelectAddress,
    PdfView,
  },
  props: {
    status: String,
    self: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      enumSignType,
      editVisible: false,
      viewMode: true,
      sigNo: '',
      tableName: 'sign_list_view',
      params: {
        index: 1,
        size: 10,
        'sign_type.in': '1,2',
      },
      loading: false,
      dialogHeight: 300,
      tableData: [],
      tableColumns: [
        { attrs: { label: '客户名称', prop: 'customer_name', minWidth: 135 } },
        { attrs: { label: '地址', prop: 'address', minWidth: 135 } },
        { attrs: { label: '负责人姓名', prop: 'corp_name', align: 'center' } },
        this.status.includes(0)
          ? { attrs: { label: '合同完成时间', prop: 'begin_sign_time', width: 100 } }
          : { attrs: { label: '签章完成时间', prop: 'end_sign_time', width: 100 } },
        { attrs: { label: '项目经理', prop: 'res_a_name' } },
        { attrs: { label: '签章类型', prop: 'signType', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', width: 120 },
          operations: [{ content: '查看', icon: 'fa fa-search', event: this.handleView }],
        },
      ],
      totalCount: 0,
      docId: '',
      viewVisible: false,
      downName: '',
      queryId: '',
    }
  },

  computed: {
    ...mapGetters(['clientId', 'userId']),
    order() {
      return this.status.includes(0) ? { begin_sign_time: 'desc' } : { end_sign_time: 'desc' }
    },
  },

  created() {
    if (this.self) {
      this.params['orWhere.res_a.eq'] = this.userId
      this.params['orWhere.res_b.eq'] = this.userId
    }
    this.params['service_id.eq'] = this.clientId
    this.params['sign_status.in'] = this.status
  },
  methods: {
    handleView(item) {
      this.docId = item.doc_id
      this.queryId = item.customer_related_id
      this.downName = `${item.signType} - ${item.customer_name}`
      this.viewVisible = true
    },
    getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData(this.serviceName, this.tableName, this.params, this.order)
        .then(res => {
          const data = res?.data ?? {}
          this.tableData = (data?.data ?? []).map(this.dataAdapter)
          this.totalCount = data?.totalCount ?? 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      item.signType = enumSignType[item.sign_type]
      return item
    },
  },
}
</script>
<style lang="scss" scoped></style>
